<template>
  <el-dialog :title="isEdit ? '修改角色' : '添加角色'" :visible="dialogVisible" width="30%" :before-close="handleClose">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm" size="mini">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入角色名称"></el-input>
      </el-form-item>
      <el-form-item label="权限" prop="region">
        <el-tree :data="data" show-checkbox :props="defaultProps" ref="tree" node-key="id" :default-checked-keys="CheckidKeys"></el-tree>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSubmit('ruleForm')">确 定</el-button>
      <el-button @click="handleCancel('ruleForm')">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { menulist, add } from '../api/role'
export default {
  name: 'DialogRole',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    isEdit: {
      type: Boolean,
      default: false,
    },
    editData: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      ruleForm: {
        name: '',
      },
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
      },
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name',
      },
      CheckidKeys: [41, 42, 43, 44, 46],
    }
  },

  mounted() {},

  methods: {
    async getMenulist() {
      let {
        data: { list },
      } = await menulist()
      this.data = list
    },
    async getRoleMenuList(query) {
      let {
        data: { list },
      } = await menulist(query)
      this.CheckidKeys = this.getCheckedKeysId(list)
    },
    getCheckedKeysId(array) {
      let checkedKeys = []
      function getKeys(array) {
        array.forEach((item) => {
          if (item.checked == 1) {
            checkedKeys.push(item.id)
          }
          if (item.children && item.children.length > 0) {
            getKeys(item.children)
          }
        })
      }
      getKeys(array)
      return checkedKeys
    },

    handleSubmit(formName) {
      //   this.handleCancel();
      // console.log(this.$refs.tree.getCheckedKeys());
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let checkedKeys = this.$refs.tree.getCheckedKeys()
          this.ruleForm.menus = checkedKeys.map((item) => {
            return { id: item }
          })
          await add(this.ruleForm)
          this.$message.success('添加成功')
          this.resetForm(formName)
          this.$emit('refreshList')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.ruleForm = {}
      this.$refs.tree.setCheckedKeys([])
      this.$refs[formName].resetFields()
    },
    handleCancel(formName = 'ruleForm') {
      let dialogVisible = false
      this.resetForm(formName)
      this.$emit('handleCancel', dialogVisible)
    },
    handleClose() {
      this.$confirm('确认关闭？')
        .then(() => {
          this.handleCancel()
        })
        .catch(() => {})
    },
  },
  created() {
    this.getMenulist()
  },
  watch: {
    isEdit(val) {
      if (val) {
        this.ruleForm = JSON.parse(JSON.stringify(this.editData))
        this.getRoleMenuList({ roleid: this.ruleForm.id })
      }
    },
  },
}
</script>
